package com.fanben100.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;

import javax.print.attribute.standard.JobMessageFromOperator;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

import com.fanben100.biz.RequestBuilder;
import com.fanben100.domain.Card;
import com.fanben100.gui.i18n.ResourceContext;
import com.fanben100.util.DateUtil;
import com.fanben100.util.IdentityUtil;
import com.fanben100.util.PasswordEncoder;
import com.fanben100.util.PropertyUtils;
import com.fanben100.util.ProtocalUtil;
import com.fanben100.util.StringUtil;

/**
 * 开卡窗口
 * 
 * @author Administrator
 * 
 */
public class OpenCardDialog2 extends JDialog {

	// Image icon = Toolkit.getDefaultToolkit().getImage(
	// "resources\\images\\logo.gif");
	// Image icon = Toolkit.getDefaultToolkit().getImage(
	// "img\\logo.gif");
	Image icon = Toolkit.getDefaultToolkit().createImage(
			this.getClass().getResource("/img/logo.gif"));

	private Boolean submitting = false;
	/**
	 * 
	 */
	private ResourceContext rt = ResourceContext.getInstance("OpenCardDialog");

	/**
	 * 会员tabpanel
	 */
	JTabbedPane vipPanel = new JTabbedPane();

	/**
	 * 会员基本信息Panel
	 */
	JPanel basicPanel = new JPanel();

	private static final long serialVersionUID = 1L;

	private JLabel cardNoLabel = new JLabel(rt.getColonString("cardNo"),
			JLabel.LEFT);
	private JTextField cardNoField = new JTextField("", 20);

	private JLabel identityNoLable = new JLabel(
			rt.getColonString("identityNo"), JLabel.LEFT);
	private JTextField identityNoField = new JTextField("", 20);

	private JLabel cardPasswordLabel = new JLabel(
			rt.getColonString("cardPassword"), JLabel.LEFT);
	private JPasswordField cardPasswordField = new JPasswordField(15);

	private JLabel cardRepeatPasswordLabel = new JLabel(
			rt.getColonString("cardRepeatPassword"), JLabel.LEFT);
	private JPasswordField cardRepeatPasswordField = new JPasswordField(15);

	private JButton registButton = new JButton(rt.getString("registButton"));

	public OpenCardDialog2(JFrame frame, String title, boolean modal) {
		super(frame, title, modal);
		this.setIconImage(icon);
		this.setSize(new Dimension(180, 100));
		this.setLocationRelativeTo(frame);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});
		try {
			jbInit();
			pack();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void jbInit() throws Exception {
		this.setResizable(false);
		JPanel vipRegistPanel = new JPanel(false);
		vipRegistPanel
				.setLayout(new BoxLayout(vipRegistPanel, BoxLayout.X_AXIS));
		registButton.setSize(new Dimension(20, 10));
		registButton.addActionListener(new SellerRegistAction());
		registButton.registerKeyboardAction(new SellerRegistAction(),
				KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		cardNoLabel.setFont(FontFactory.creatFont());
		identityNoLable.setFont(FontFactory.creatFont());
		cardPasswordLabel.setFont(FontFactory.creatFont());
		cardRepeatPasswordLabel.setFont(FontFactory.creatFont());
		cardPasswordField.setEchoChar('*');
		cardRepeatPasswordField.setEchoChar('*');

		JPanel labelPanel = new JPanel(false);
		labelPanel.setLayout(new GridLayout(4, 1, 20, 10));
		labelPanel.add(cardNoLabel);
		labelPanel.add(identityNoLable);
		labelPanel.add(cardPasswordLabel);
		labelPanel.add(cardRepeatPasswordLabel);

		cardNoField.setFont(FontFactory.creatFont());
		cardNoField.setSize(new Dimension(20, 100));
		identityNoField.setFont(FontFactory.creatFont());
		cardPasswordField.setFont(FontFactory.creatFont());
		cardRepeatPasswordField.setFont(FontFactory.creatFont());

		JPanel fieldPanel = new JPanel(false);
		fieldPanel.setLayout(new GridLayout(4, 1, 20, 10));
		fieldPanel.add(cardNoField);
		fieldPanel.add(identityNoField);
		fieldPanel.add(cardPasswordField);
		fieldPanel.add(cardRepeatPasswordField);

		getContentPane().setLayout(new BorderLayout());

		vipRegistPanel.add(labelPanel);
		vipRegistPanel.add(fieldPanel);
		// this.getContentPane().add(sellerRegistPanel, BorderLayout.CENTER);
		// this.getContentPane().add(registButton, BorderLayout.PAGE_END);
		basicPanel.setLayout(new BorderLayout());
		basicPanel.add(vipRegistPanel, BorderLayout.CENTER);
		basicPanel.add(registButton, BorderLayout.PAGE_END);
		vipPanel.add("基本", basicPanel);
		this.getContentPane().add(vipPanel);
		getContentPane().setSize(new Dimension(500, 400));
	}

	/**
	 * 会员注册Action
	 * 
	 * @author Administrator
	 * 
	 */
	private synchronized void submitCardRegist() {
		if (!submitting) {
			submitting = true;
			Card registCard = validCardInput();
			if (registCard != null) {
				String submitResult = null;
				// TODO 此处访问地址需要更改
				RequestBuilder requestBuilder = new RequestBuilder(
						"http://www.fanben100.net/shop.php");
				requestBuilder.build("class", "super");
				requestBuilder.build("method", "do_form");
				try {
					submitResult = StringUtil.trimAll(ProtocalUtil.httpRequest(
							new URL(requestBuilder.toString()), null));
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
					submitResult = "请求超时，请检查网络连通情况 "
							+ DateUtil.getCurrentDate();
				}
				if ("操作成功".equals(submitResult)) {
					// rightConsole("注册成功!会员卡号:" + registCard.getCardNo());
					submitting = false;
				} else {
					// errorConsole(submitResult);
					submitting = false;
				}
			}
			submitting = false;
		}
	}

	private class SellerRegistAction implements ActionListener, Runnable {

		public void actionPerformed(ActionEvent e) {
			Thread td = new Thread(this);
			td.start();
		}

		public void run() {
			synchronized (submitting) {
				registButton.setEnabled(false);
				submitCardRegist();
				registButton.setEnabled(true);
			}
		}

	}

	private Card validCardInput() {
		String cardNo = cardNoField.getText();
		if (cardNo.length() < 6) {
			JOptionPane.showMessageDialog(this, "卡号格式不正确");
			return null;
		}
		String checkResult = "";
		String identityNo = identityNoField.getText();
		try {
			checkResult = IdentityUtil.IDCardValidate(identityNo);
		} catch (ParseException e) {
			checkResult = "身份证号生日格式不正确";
			e.printStackTrace();
		}
		if (checkResult != "") {
			JOptionPane.showMessageDialog(this, checkResult);
			return null;
		}
		String cardPassword = new String(cardPasswordField.getPassword());
		String cardRepeatPassword = new String(
				cardRepeatPasswordField.getPassword());
		if (cardPassword.length() != 6 || cardRepeatPassword.length() != 6) {
			JOptionPane.showMessageDialog(this, "开卡密码或重复密码格式不对");
			return null;
		}
		if (!cardPassword.equals(cardRepeatPassword)) {
			JOptionPane.showMessageDialog(this, "重复密码和密码不匹配");
			return null;
		}

		Card card = new Card();
		card.setCardNo(cardNo);
		card.setCardPassword(PasswordEncoder.getMD5(cardPassword.getBytes()));
		card.setIdentityNo(identityNo);
		return card;
	}
}
